﻿Imports System.Web
Imports System.Net
Imports System.Text
Imports System.IO

Public Class Mylot
    Private _sessionID As String
    Const _commentURL As String = "http://www.mylot.com/nr/newcomment.aspx?aid={0}&qid={1}&p=1"
    Const _ResponseURL As String = "http://www.mylot.com/nr/newresponse.aspx?p=1&qid={0}"

    Sub New(ByVal sessionID As String)
        _sessionID = sessionID
    End Sub

    Sub PostResponse(ByVal questionID As Long, ByVal value As String)
        Dim url = String.Format(_ResponseURL, questionID)
        Dim d As New System.Collections.Specialized.NameValueCollection
        d.Add("ctl00$cphMainContent$tbAnswer", value)
        PostData(url, d)
    End Sub

    Sub PostComment(ByVal questionID As Long, ByVal answerID As Long, ByVal value As String)
        Dim url = String.Format(_commentURL, answerID, questionID)
        Dim d As New System.Collections.Specialized.NameValueCollection
        d.Add("ctl00$cphMainContent$tbComment", value)
        PostData(url, d)

        'Dim url = String.Format(_commentURL, questionID, answerID)

        'Dim wc As New System.Net.WebClient
        'wc.Headers.Add("Cookie", "ASP.NET_SessionId=" & _sessionID)
        'wc.Headers.Add("Accept-Encoding", "gzip,deflate")


        'Dim dt As String
        'Dim d1 = wc.DownloadData(url)
        'dt = UnZip(d1)

        'Dim c As String = ""
        'c &= getFormDataString("__EVENTTARGET", "ctl00$cphMainContent$lbStart")
        'c &= getFormDataString("__EVENTARGUMENT", "")

        'c &= getFormDataString("__VIEWSTATE", getViewState(dt))

        ''c &= getFormDataString("ctl00$cphMainContent$ctl00$mTmp", "0")
        ''c &= getFormDataString("ctl00$cphMainContent$ctl00$tbSearch", "Search the Web")
        'c &= getFormDataString("ctl00$cphMainContent$tbComment", value)
        ''c &= getFormDataString("ctl00$cphMainContent$ctl02$fuPhoto", "", True)
        ''c &= getFormDataString("ctl00$cphMainContent$ctl02$tbPhotoCaption", "")
        ''c &= getFormDataString("ctl00$cphMainContent$ctl02$tbPhotoDetails", "")
        ''c &= getFormDataString("ctl00$cphMainContent$ctl02$tbPhotoTags", "")
        'c &= getFormDataString("__EVENTVALIDATION", getEventValidation(dt), , True)

        'Dim req As HttpWebRequest = WebRequest.Create(url)

        'Dim buffer = Encoding.UTF8.GetBytes(c)

        'req.Method = "POST"
        'req.ContentType = "multipart/form-data; boundary=---------------------------32170203159576"
        'req.ContentLength = buffer.Length
        'req.CookieContainer = New CookieContainer()

        'req.Headers.Add("Accept-Encoding", "gzip,deflate")

        'req.CookieContainer.Add(New System.Net.Cookie("ASP.NET_SessionId", _sessionID) With {.Domain = "www.mylot.com"})

        'Dim reqst As Stream = req.GetRequestStream() '; // add form data to request stream
        'reqst.Write(buffer, 0, buffer.Length)
        'reqst.Flush()
        'reqst.Close()
        'Dim res As HttpWebResponse = req.GetResponse()

        'Dim scc As New System.IO.StreamReader(res.GetResponseStream)

        'Dim ckc = scc.ReadToEnd


        'Dim b = From T In scc.ReadToEnd Select T

    End Sub

    Private Sub PostData(ByVal url As String, ByVal v As System.Collections.Specialized.NameValueCollection)

        Dim wc As New System.Net.WebClient
        wc.Headers.Add("Cookie", "ASP.NET_SessionId=" & _sessionID)
        wc.Headers.Add("Accept-Encoding", "gzip,deflate")


        Dim dt As String
        Dim d1 = wc.DownloadData(url)
        dt = Utils.UnZip(d1)

        Dim c As String = ""
        c &= getFormDataString("__EVENTTARGET", "ctl00$cphMainContent$lbStart")
        c &= getFormDataString("__EVENTARGUMENT", "")

        c &= getFormDataString("__VIEWSTATE", Utils.getViewState(dt))

        'c &= getFormDataString("ctl00$cphMainContent$ctl00$mTmp", "0")
        'c &= getFormDataString("ctl00$cphMainContent$ctl00$tbSearch", "Search the Web")


        For i = 0 To v.Count - 1
            c &= getFormDataString(v.Keys(i), v(i))
        Next

        'c &= getFormDataString("ctl00$cphMainContent$tbComment", value)
        'c &= getFormDataString("ctl00$cphMainContent$ctl02$fuPhoto", "", True)
        'c &= getFormDataString("ctl00$cphMainContent$ctl02$tbPhotoCaption", "")
        'c &= getFormDataString("ctl00$cphMainContent$ctl02$tbPhotoDetails", "")
        'c &= getFormDataString("ctl00$cphMainContent$ctl02$tbPhotoTags", "")
        c &= getFormDataString("__EVENTVALIDATION", Utils.getEventValidation(dt), , True)

        Dim req As HttpWebRequest = WebRequest.Create(url)

        Dim buffer = Encoding.UTF8.GetBytes(c)

        req.Method = "POST"
        req.ContentType = "multipart/form-data; boundary=---------------------------32170203159576"
        req.ContentLength = buffer.Length
        req.CookieContainer = New CookieContainer()

        req.Headers.Add("Accept-Encoding", "gzip,deflate")

        req.CookieContainer.Add(New System.Net.Cookie("ASP.NET_SessionId", _sessionID) With {.Domain = "www.mylot.com"})

        Dim reqst As Stream = req.GetRequestStream() '; // add form data to request stream
        reqst.Write(buffer, 0, buffer.Length)
        reqst.Flush()
        reqst.Close()
        Dim res As HttpWebResponse = req.GetResponse()

        Dim scc As New System.IO.StreamReader(res.GetResponseStream)

        Dim ckc = scc.ReadToEnd

    End Sub

    Private Function getFormDataString(ByVal n As String, _
                                 ByVal v As String, _
                                 Optional ByVal b As Boolean = False, _
                                 Optional ByVal isend As Boolean = False) As String

        Dim l1 = "-----------------------------32170203159576"
        Dim l2 = "Content-Disposition: form-data; name=""{0}"""

        If b Then
            l2 &= "; filename=""""" & vbCrLf & "Content-Type: application/octet-stream"
        End If

        Dim l3 = "{1}"

        Dim s = String.Format(l1 & vbCrLf & l2 & vbCrLf & vbCrLf & l3 & vbCrLf, n, v)

        If isend Then
            s = s & "-----------------------------32170203159576--"
        End If

        Return s

    End Function

End Class